﻿using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HenrryTools.WinFormTools
{
    public partial class UrlWinForm : Form
    {
        public UrlWinForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            txtRoot.Text = "";
        }
        /// <summary>
        /// 缓存并且判断最常使用得密钥  IsClipboard默认为True复制
        /// </summary>
        public void DefaultLoadValue(string Message, bool IsClipboard)
        {
            Properties.Settings.Default.URLWinFormRoot = txtRoot.Text;//保存在Settings
            Properties.Settings.Default.Save();//保存

            if (IsClipboard)
            {
                Clipboard.SetDataObject(txtRoot.Text);//复制
            }

            labMessage.Text = Message;
            labMessage.ForeColor = Color.Green;
        }
        /// <summary>
        /// URL编码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUrlEnCode_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRoot.Text))
            {
                labMessage.Text = "入参为空，请输入!";
                labMessage.ForeColor = Color.Red; return;
            }
            try
            {
                Encoding Encoding = System.Text.Encoding.UTF8;
                Encoding = SelectEncoding(CbxSelect.Text);
                txtRoot.Text = System.Web.HttpUtility.UrlEncode(txtRoot.Text, Encoding);
                DefaultLoadValue("URLEnCode编码成功，剪切板已复制!", true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
          
        }
        /// <summary>
        /// 判断select的值
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public Encoding SelectEncoding(string Code)
        {
            Encoding Encoding = System.Text.Encoding.UTF8;
            switch (Code)
            {
                case "UTF-8":
                    Encoding = System.Text.Encoding.UTF8;
                    break;
                case "UTF-7":
                    Encoding = System.Text.Encoding.UTF7;
                    break;
                case "UTF-32":
                    Encoding = System.Text.Encoding.UTF32;
                    break;
                case "Unicode":
                    Encoding = System.Text.Encoding.Unicode;
                    break;
                default:
                    Encoding = System.Text.Encoding.UTF8;
                    break;
            }
            return Encoding;
        }
        /// <summary>
        /// 解码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDeCode_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRoot.Text))
            {
                labMessage.Text = "入参为空，请输入!";
                labMessage.ForeColor = Color.Red; return;
            }
            try
            {
                Encoding Encoding = System.Text.Encoding.UTF8;
                Encoding = SelectEncoding(CbxSelect.Text);
                txtRoot.Text = System.Web.HttpUtility.UrlEncode(txtRoot.Text, Encoding);
                DefaultLoadValue("URLDeCode编码成功，剪切板已复制!", true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
          
        }
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UrlWinForm_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(Properties.Settings.Default.URLWinFormRoot))
            {
                txtRoot.Text = Properties.Settings.Default.URLWinFormRoot;
            }
        }
    }
}
